<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>src\utils.js - GIAnT API</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.9.1/build/cssgrids/cssgrids-min.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
    <link rel="icon" href="../assets/favicon.ico">
    <script src="http://yui.yahooapis.com/combo?3.9.1/build/yui/yui-min.js"></script>
</head>
<body class="yui3-skin-sam">

<div id="doc">
    <div id="hd" class="yui3-g header">
        <div class="yui3-u-3-4">
                <h1><img src="../assets/css/logo.png" title="GIAnT API" width="117" height="52"></h1>
        </div>
        <div class="yui3-u-1-4 version">
            <em>API Docs for: 1.0.0</em>
        </div>
    </div>
    <div id="bd" class="yui3-g">

        <div class="yui3-u-1-4">
            <div id="docs-sidebar" class="sidebar apidocs">
                <div id="api-list">
                    <h2 class="off-left">APIs</h2>
                    <div id="api-tabview" class="tabview">
                        <ul class="tabs">
                            <li><a href="#api-classes">Classes</a></li>
                            <li><a href="#api-modules">Modules</a></li>
                        </ul>
                
                        <div id="api-tabview-filter">
                            <input type="search" id="api-filter" placeholder="Type to filter APIs">
                        </div>
                
                        <div id="api-tabview-panel">
                            <ul id="api-classes" class="apis classes">
                                <li><a href="../classes/Atom.html">Atom</a></li>
                                <li><a href="../classes/Codec.html">Codec</a></li>
                                <li><a href="../classes/Constraints.html">Constraints</a></li>
                                <li><a href="../classes/Database.html">Database</a></li>
                                <li><a href="../classes/Exif.html">Exif</a></li>
                                <li><a href="../classes/Export.html">Export</a></li>
                                <li><a href="../classes/Heatmap.html">Heatmap</a></li>
                                <li><a href="../classes/Server.html">Server</a></li>
                                <li><a href="../classes/Settings.html">Settings</a></li>
                                <li><a href="../classes/Utils.html">Utils</a></li>
                            </ul>
                
                
                            <ul id="api-modules" class="apis modules">
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="yui3-u-3-4">
                <div id="api-options">
                    Show:
                    <label for="api-show-inherited">
                        <input type="checkbox" id="api-show-inherited" checked>
                        Inherited
                    </label>
            
                    <label for="api-show-protected">
                        <input type="checkbox" id="api-show-protected">
                        Protected
                    </label>
            
                    <label for="api-show-private">
                        <input type="checkbox" id="api-show-private">
                        Private
                    </label>
                    <label for="api-show-deprecated">
                        <input type="checkbox" id="api-show-deprecated">
                        Deprecated
                    </label>
            
                </div>
            
            <div class="apidocs">
                <div id="docs-main">
                    <div class="content">
<h1 class="file-heading">File: src\utils.js</h1>

<div class="file">
    <pre class="code prettyprint linenums">
/**
* A collection of utility functions
*
* @class Utils
*/


var crypto = require(&#x27;crypto&#x27;);
var fs = require(&#x27;fs&#x27;);
var path = require(&#x27;path&#x27;);


var javascript_demo_constraint = &quot;// session = a neo4j session\n&quot; +
&quot;// session.run(cypher_string) returns a promise (see the docs)\n&quot; +
&quot;new Promise(function(resolve, reject){\n&quot; +
&quot;    var variables = {\&quot;fragment_id\&quot;: fragment_id};\n&quot; +
&quot;    session.run(\&quot;MATCH(f:Fragment)-[]-(t:Token {value: &#x27;Token&#x27;}) WHERE ID(f) = {fragment_id} RETURN t.value as value;\&quot;, variables)\n&quot; +
&quot;        .then(function(result){ \n&quot; +
&quot;            var value;\n&quot; +
&quot;            result.records.forEach(function(res){\n&quot; +
&quot;                value = res.get(&#x27;value&#x27;);\n&quot; +
&quot;                if (value === \&quot;Token\&quot;) {\n&quot; +
&quot;                    reject(\&quot;There was a token called Token.\&quot;);\n&quot; +
&quot;                }\n&quot; +
&quot;            });\n&quot; +
&quot;            resolve();\n&quot; +
&quot;    }).catch(function(err){\n&quot; +
&quot;        reject(err);\n&quot; +
&quot;    });\n&quot; +
&quot;});\n&quot;;

/**
* SHA1 of the file
 *
* @method hash_of_file_content
* @param file_path {string}
* @return {string|null}
*/
function hash_of_file_content(file_path) {
    try {
        var data = fs.readFileSync(__dirname + &#x27;/&#x27; + file_path);
        return crypto.createHash(&#x27;sha1&#x27;).update(data.toString()).digest(&#x27;hex&#x27;).toString();
    } catch (e) {
        return null;
    }
}

/**
* SHA1 of the xml file
 *
* @method hash_xml_fragment
* @param fragment_id {Number}
* @return {string|null}
*/
function hash_xml_fragment (fragment_id) {
    return hash_of_file_content(&#x27;../media/uploaded_xmls/&#x27; + fragment_id + &#x27;.xml&#x27;);
}

/**
* Delete image from disk
 *
* @method remove_image
* @param file_path {string}
* @return return_code {Number}
*/
function remove_image(file_path) {
    return fs.unlinkSync(path.join(__dirname, &#x27;..&#x27;, &#x27;media&#x27;, &#x27;uploaded_images&#x27;, file_path));
}

/**
* Differentiates between Token and Group
 *
* @method token_type_mapping
* @param token_type {string}
* @return {string}
*/
function token_type_mapping(token_type) {
    if ([&#x27;token&#x27;, &#x27;modification&#x27;, &#x27;symbol&#x27;].includes(token_type)) {
        return &#x27;Token&#x27;;
    }
    if ([&#x27;frame&#x27;, &#x27;comment&#x27;, &#x27;blanco&#x27;].includes(token_type)) {
        return &#x27;Group&#x27;;
    }
    return null;
}


/**
 * chains a list of functions (that return promises) and executes them in the right order
 * [function() {return Promise.resolve();}, function() {return Promise.resolve();}]
 *
 * @method chain_promises
 * @param funcs is an array of functions returning promises
 * @return {Promise}
 */
function chain_promises(funcs) {
    if (funcs.length &lt; 1) {
        return Promise.resolve();
    }
    var i = 0;
    return chain_executor(funcs, i);
}

/**
 * Recursive help method for chain_promises
 * 1) executes a function that returns a promise (no params allowed)
 * 2) chains itself to the success resolve of the promise
 *
 * @method chain_executor
 * @param funcs is an array of functions returning promises
 * @param i is the current working index
 */
function chain_executor(funcs, i) {
    var promise = funcs[i]();
    return promise.then(function(){
        console.log(i);
        if (funcs.length &gt; i+1) {
            return chain_executor(funcs, i+1);
        } else {
            return Promise.resolve();
        }
    })
}

module.exports = {
    &#x27;javascript_demo_constraint&#x27;:javascript_demo_constraint,
    &#x27;hash_of_file_content&#x27;: hash_of_file_content,
    &#x27;hash_xml_fragment&#x27;: hash_xml_fragment,
    &#x27;remove_image&#x27;: remove_image,
    &#x27;token_type_mapping&#x27;: token_type_mapping,
    &#x27;chain_promises&#x27;: chain_promises
};

    </pre>
</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script src="../assets/js/yui-prettify.js"></script>
<script src="../assets/../api.js"></script>
<script src="../assets/js/api-filter.js"></script>
<script src="../assets/js/api-list.js"></script>
<script src="../assets/js/api-search.js"></script>
<script src="../assets/js/apidocs.js"></script>
</body>
</html>
